package com.wangzhen.spring.ioc.dependcy.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Terminate implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware,
         InitializingBean, SmartInitializingSingleton, DisposableBean {

    private String whoAmI;

    private String beanName;
    private ClassLoader classLoader;
    private BeanFactory beanFactory;
    private Environment environment;

    @PostConstruct
    public void init() {
        System.out.println(this);
        this.whoAmI = "@PostConstruct method changed whoAmI value";
        System.out.println(this);
    }

    @PreDestroy
    public void preDestory() {
        this.whoAmI = "@preDestory method changed whoAmI value";
        System.out.println(this);
    }

    public String getWhoAmI() {
        return whoAmI;
    }

    public void setWhoAmI(String whoAmI) {
        this.whoAmI = whoAmI;
    }



    @Override
    public String toString() {
        return "Terminate{" +
                "whoAmI='" + whoAmI + '\'' +
                '}';
    }

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public String getBeanName() {
        return beanName;
    }

    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public Environment getEnvironment() {
        return environment;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.whoAmI = "afterPropertiesSet method changed whoAmI value";
        System.out.println(this);
    }

    @Override
    public void afterSingletonsInstantiated() {
        this.whoAmI = "afterSingletonsInstantiated methoid changed whoAmi value";
        System.out.println(this);
    }

    @Override
    public void destroy() throws Exception {
        this.whoAmI = "destory method changed whoAmi value";
        System.out.println(this);
    }
}
